package com.lzm.back.controller;

import com.github.pagehelper.PageInfo;
import com.lzm.back.common.MyResult;
import com.lzm.back.entity.Score;
import com.lzm.back.entity.User;
import com.lzm.back.service.ScoreService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

//评分情况
@RestController
@RequestMapping("/Score")
public class ScoreController {

    // 新增评分
    @PostMapping("/add")
    public MyResult addScore(@RequestBody Score score) {
        scoreService.addScore(score);
        return MyResult.success();
    }

    // 批量删除
    @DeleteMapping("/batch")
    public MyResult deleteBatch(@RequestBody List<Integer> ids) {
        scoreService.deleteBatch(ids);
        return MyResult.success();
    }
    //删除单个
    @DeleteMapping("/{id}")
    public MyResult deleteScore(@PathVariable Long id) {
        scoreService.deleteScore(id);
        return MyResult.success();
    }

    // 更新评分
    @PutMapping("/update")
    public MyResult updateScore(@RequestBody Score score) {
        scoreService.updateScore(score);
        return MyResult.success();
    }

    @Resource
    private ScoreService scoreService;

    @GetMapping("/selectScore")
    public MyResult selectScore(Score score,
                                @RequestParam(required = false) Integer id,
                                @RequestParam(required = false) String username,
                                @RequestParam(required = false) String comment,
                                @RequestParam(required = false) String role, // 新增参数接收
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {

        PageInfo<Score> pageInfo = scoreService.selectScore(id, username, role, comment, pageNum, pageSize, score);
        return MyResult.success(pageInfo);
    }

    @GetMapping("/selectScoreGroupByEvent")
    public MyResult selectScoreGroupByEvent(Score score,
                                @RequestParam(required = false) Integer id,
                                @RequestParam(required = false) String username,
                                @RequestParam(required = false) String comment,
                                @RequestParam(required = false) String role, // 新增参数接收
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {

        PageInfo<Score> pageInfo = scoreService.selectScoreGroupByEvent(id, username, role, comment, pageNum, pageSize, score);
        return MyResult.success(pageInfo);
    }

    //个人最高分和平均分
    @GetMapping("/selectMaxAndAvg")
    public MyResult selectMaxAndAvg(@RequestParam(required = false)Long userId,
                                    @RequestParam(required = false)Long eventId) {
        Score score = scoreService.selectMaxAndAvg(userId, eventId);
        return MyResult.success(score);
    }

    //获取最高分对应的参赛者
    @GetMapping("/selectMaxScoreParticipant")
    public MyResult selectMaxScoreParticipant(@RequestParam(required = false)Long eventId,
                                              @RequestParam(required = false) BigDecimal score) {
        List<User> users = scoreService.selectMaxScoreParticipant(eventId,score);
        return MyResult.success(users);
    }

}
